package dk.contix.eclipse.hudson.views;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.action.Action;
import org.eclipse.swt.widgets.Display;

public final class RefreshAction extends Action {
	private final HudsonView hudsonView;

	public RefreshAction(HudsonView hudsonView) {
		this.hudsonView = hudsonView;
	}

	@Override
	public void run() {
		Job refresh = new Job("Refreshing Hudson status") {
			@Override
			protected IStatus run(IProgressMonitor monitor) {
				monitor.beginTask("Refreshing Hudson status", 1);
				try {
					hudsonView.jobContentProvider.refresh();
					
					Display.getDefault().asyncExec(new Runnable() {
						@Override
						public void run() {
							hudsonView.viewer.refresh();
						}
					});
				} finally {
					monitor.done();
				}
				return Status.OK_STATUS;
			}
		};
		refresh.schedule();
	}
}
